package com.cc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cc.domain.parm.LiverWaterParm;
import com.cc.domain.parm.WaterParm;
import com.cc.domain.pojo.House;
import com.cc.domain.pojo.LiverAndHouse;
import com.cc.domain.pojo.Water;
import com.cc.domain.pojo.Wy;
import com.cc.mapper.HouseMapper;
import com.cc.mapper.LiverAndHouseMapper;
import com.cc.mapper.WyMapper;
import com.cc.service.WaterService;
import com.cc.service.WyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 水费管理ServiceImpl层
 */
@Service
public class WyServiceImpl extends ServiceImpl<WyMapper, Wy> implements WyService {

    @Autowired
    private LiverAndHouseMapper liverAndHouseMapper;
    @Autowired
    private HouseMapper houseMapper;

    //新增
    @Override
    public boolean saveWy(Wy wy) {
        //根据房屋id查询业主
        QueryWrapper<LiverAndHouse> liverAndHouseWrapper = new QueryWrapper<>();
        liverAndHouseWrapper.lambda().eq(LiverAndHouse::getHouseId,wy.getHouseId())
                .eq(LiverAndHouse::getLiverHouseStatus,"0");
        LiverAndHouse liverAndHouse = liverAndHouseMapper.selectOne(liverAndHouseWrapper);
        House house = houseMapper.selectById(wy.getHouseId());
        // 计算物业费
        BigDecimal money = BigDecimal.valueOf(Double.parseDouble(house.getHouseArea()) * wy.getWyPrice() * 12);
        wy.setWyMoney(money);
        if (liverAndHouse != null){
            //查询出的业主id设置到水费里
            wy.setLiverId(liverAndHouse.getLiverId());
            //保存数据
            baseMapper.insert(wy);
            return true;
        }
        return false;
    }

    //编辑
    @Override
    public boolean updateWy(Wy wy) {
        //根据房屋id查询业主
        QueryWrapper<LiverAndHouse> liverAndHouseWrapper = new QueryWrapper<>();
        liverAndHouseWrapper.lambda().eq(LiverAndHouse::getHouseId,wy.getHouseId())
                .eq(LiverAndHouse::getLiverHouseStatus,"0");
        LiverAndHouse liverAndHouse = liverAndHouseMapper.selectOne(liverAndHouseWrapper);

        House house = houseMapper.selectById(wy.getHouseId());
        // 计算物业费
        BigDecimal money = BigDecimal.valueOf(Double.parseDouble(house.getHouseArea()) * wy.getWyPrice() * 12);
        if (liverAndHouse != null) {
            //查询出的业主id设置到水费里
            wy.setLiverId(liverAndHouse.getLiverId());
            wy.setWyMoney(money);
            //修改数据
            baseMapper.updateById(wy);
            return true;
        }
        return false;
    }

    //删除
    @Override
    public boolean removeWyById(Integer wyId) {
        //如果已经缴费，就不能删除
        QueryWrapper<Wy> wyWrapper = new QueryWrapper<>();
        wyWrapper.lambda().eq(Wy::getWyId,wyId)
                .eq(Wy::getWyStatus,"1");
        Long count = baseMapper.selectCount(wyWrapper);
        if (count <= 0){
            //未缴费的记录可以执行删除
            baseMapper.deleteById(wyId);
            return true;
        }
        return false;
    }

    //分页查询
    @Override
    public IPage<Wy> getList(WaterParm waterParm) {
        //构造分页对象
        IPage<Wy> page = new Page<>();
        page.setCurrent(waterParm.getCurrentPage());
        page.setSize(waterParm.getPageSize());
        return baseMapper.getWyList(page, waterParm.getLiverName(), waterParm.getHouseNum());
    }

    //物业费记录查询
    @Override
    public IPage<Wy> getLiverWyList(LiverWaterParm liverWaterParm) {
        //后遭分页对象
        IPage<Wy> page = new Page<>();
        page.setCurrent(liverWaterParm.getCurrentPage());
        page.setSize(liverWaterParm.getPageSize());
        //构造条件构造器
        QueryWrapper<Wy> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Wy::getLiverId,liverWaterParm.getLiverId());
        return baseMapper.selectPage(page,wrapper);
    }
}
